package com.ltv.xiangqibook.web;

import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Properties;

public class MessageService {
	private static MessageService instance;
	private Properties en;
	private Properties vi;
	
	private MessageService() {
		en = new Properties();
		vi = new Properties();
	}
	
	public void init() throws Exception {
		InputStream is = null;
        InputStreamReader isr = null;
        try {
        	is = new FileInputStream("messages/messages_en.txt");
            isr = new InputStreamReader(is, "utf-8");
            en.load(isr);
        } finally {
            if (is != null) {
				is.close();
				is = null;
            }
            if (isr != null) {
				isr.close();
				isr = null;
            }
        }
        
        try {
        	is = new FileInputStream("messages/messages_vi.txt");
            isr = new InputStreamReader(is, "utf-8");
            vi.load(isr);
        } finally {
            if (is != null) {
				is.close();
				is = null;
            }
            if (isr != null) {
				isr.close();
				isr = null;
            }
        }
	}
	
	public static MessageService getInstance() {
		if (instance == null) {
			instance = new MessageService();
		}
		
		return instance;
	}
	
	public String getMessage(String key, String language) {
		if (language.equalsIgnoreCase("vi")) {
			return vi.getProperty(key);
		}
		
		return en.getProperty(key);
	}
}
